home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: newshub.ariel.cs.yorku.ca!cs911108
- From: cs911108@ariel.cs.yorku.ca (IAN J COLOMBY)
- Subject: Function overloading problem
- X-Nntp-Posting-Host: red
- Message-ID: <DLBxoH.GLw@ariel.cs.yorku.ca>
- Sender: news@ariel.cs.yorku.ca (*)
- Organization: York University, Dept. of Computer Science
- Date: Wed, 17 Jan 1996 14:31:28 GMT
-
- I have the following code which in which the function call of f() inside
- function g() doesn't behave the way I expected it to.
-
- class B;
- class C;
-
- class A
- {
- virtual void f(B*);
- virtual void f(C*);
- };
-
- class B: public A
- {
- void f(B*);
- void f(C*);
- };
-
- class C: public A
- {
- void f(B*);
- void f(C*);
- };
-
-
- void g(A* a1, A* a2)
- {
- a1->f(a2);
- }
-
- The above code first of all won't compile unless I insert the following
- line of code into each class definition:
- vitrual void f(A*) in class A or void f(A*) in classes B & C.
-
- The next problem is that the above line f(A*) is the function that always
- gets called in the class of a1 no matter what a2 is in function g(). a1
- and a2 will never be of class A, they will only be of either class B or
- class C, so I'm not sure why the the call a1->f(a2) always calls the
- function f(A*) in the class of a1, and not f(B*) or f(C*) depending on
- what class a2 is.
-
- Any help in solving this problem would be appreciated.
-
- Ian.
-
-